package cn.homework.delayMessage;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("MQ-message-Consumer");
        //地址
        consumer.setNamesrvAddr("127.0.0.1:9876");
        // 设置消费组群
        consumer.subscribe("delay-message","*");
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
               try {
                   System.out.println(list.toString());
                   list.forEach(list1-> System.out.println(new String(list1.getBody())));
                   System.out.println("消费完成");
                   return  ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
               }catch (Exception e){
                   return ConsumeConcurrentlyStatus.RECONSUME_LATER;
               }
            }
        });

        consumer.start();



    }
}
